package com.zgz.park.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zgz.park.common.utils.MyMD5Util;
import com.zgz.park.pojo.Admin;
import com.zgz.park.pojo.User;
import com.zgz.park.mapper.UserMapper;
import com.zgz.park.pojo.common.CommonPageReq;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author willie
 * @since 2022-12-10
 */
@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public User login(String userName, String password){
        User loginUser = userMapper.login(userName,password);
        return loginUser;
    }

    public IPage<User> getUserListByPage(CommonPageReq queryFilter){
        Map<String, Object> params = queryFilter.getParams();

        IPage<User> userListByPage = userMapper.getUserListByPage(
                new Page<User>(queryFilter.getPageNo(), queryFilter.getPageSize()), params);
        return userListByPage;
    }

    public User getUserById(String id){
        User user = userMapper.selectById(id);
        return user;
    }

    public boolean saveSubmit(Admin vo) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        //查询是否有该数据
        User dbInfo = userMapper.selectById(vo.getId());
        boolean isUpdate = false;
        if(dbInfo != null){
            isUpdate = true;
        }else{
            isUpdate = false;
        }
        User user = new User();


        if(isUpdate){
            //更新数据
            BeanUtils.copyProperties(vo,user);
            //判断密码是否变更
            if(dbInfo.getPassword().equals(vo.getPassword())){
                user.setPassword(dbInfo.getPassword());
            }else{
                String md5PasswordVo = MyMD5Util.getEncryptedPwd(vo.getPassword());
                user.setPassword(md5PasswordVo);
            }
            userMapper.updateById(user);
        }else{
            //设置密码
            if(vo.getPassword() != null && vo.getPassword() != ""){
                String md5Password = MyMD5Util.getEncryptedPwd(vo.getPassword());
                user.setPassword(md5Password);
            }
            //新增数据
            user.setNickName(vo.getNickName());
            user.setMobile(vo.getMobile());
            user.setUserName(vo.getUserName());
            user.setSex(vo.getSex());
            user.setAge(vo.getAge());
            user.setAvatar(vo.getAvatar());
            user.setPlateNum(vo.getPlateNum());
            user.setIsDisabled(false);
            user.setIsDeleted(false);
            userMapper.insert(user);
        }
        return true;
    }

    /**
     * 禁用或解禁
     * @param id
     * @return
     */
    public boolean invalidOrRecovery(String id) {

        //查询是否有该数据
        User dbInfo = userMapper.selectById(id);

        if(dbInfo != null){
            if(!dbInfo.getIsDeleted()){
                User user = new User();
                BeanUtils.copyProperties(dbInfo,user);
                if(dbInfo.getIsDisabled()){
                    user.setIsDisabled(false);
                }else{
                    user.setIsDisabled(true);
                }
                userMapper.updateById(user);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }

    }

    public boolean deleteById(String id) {

        //查询是否有该数据
        User dbInfo = userMapper.selectById(id);
        if(dbInfo != null){
            User user = new User();
            BeanUtils.copyProperties(dbInfo,user);
            user.setIsDeleted(true);
            userMapper.updateById(user);
            return true;
        }else{
            return false;
        }
    }
}
